package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Mapper
public interface OrderMapper {

    int insert(Orders orders);

    /**
     * 根据订单号查询订单
     *
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     *
     * @param orders
     */
    void update(Orders orders);

    /**
     * 用于替换微信支付更新数据库状态的问题
     *
     * @param orderStatus
     * @param orderPaidStatus
     */

    @Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{check_out_time} where id = #{id}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime check_out_time, Long id);

    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);

    @Select("select count(id) from orders where status = #{status}")
    Integer countStatus(Integer toBeConfirmed);

    /*
     * 查询超时订单
     * */
    @Select("select * from orders where orders.order_time< #{ordertime} and status = #{status} ")
    List<Orders> getBystatusAndTime(Integer status, LocalDateTime ordertime);


    @Select("select sum(amount) from orders where order_time > #{localBeginTime} and order_time < #{localEndTime} and status = #{i}")
    Double sumByTimeAndStatus(LocalDateTime localBeginTime, LocalDateTime localEndTime, int i);

    /*
     * 统计所有订单
     * */
    @Select("select count(id) from orders")
    Integer getOrderCount();


    /*
     * 统计有效订单总数
     * */
    @Select("select count(id) from orders where status=5")
    Integer getValidOrderCount();

    /*
     * 统计当日内订单总数
     * */
    @Select("select count(id) from orders where  order_time> #{localBeginTime} and order_time < #{localEndTime}")
    Integer countEveryday(LocalDateTime localBeginTime, LocalDateTime localEndTime);

    /*
     * 统计当日内有效订单总数
     * */
    @Select("select count(id) from orders where order_time> #{localBeginTime} and order_time < #{localEndTime} and status=5")
    Integer getValidOrderCountEveryday(LocalDateTime localBeginTime, LocalDateTime localEndTime);

    /*
     * 获取前十名的名称以及销量
     * */
    List<GoodsSalesDTO> getGoodsSalesEveryday(LocalDateTime localBeginTime, LocalDateTime localEndTime);

    /*
     * 查询指定日期内的营业额
     * */
    //SELECT SUM(amount)  FROM orders WHERE status = 5 AND order_time >'2023-10-15 00:00:00'AND order_time<'2023-10-15 23:59:59'
    @Select("SELECT SUM(amount)  FROM orders WHERE status = 5 AND order_time > #{localBeginTime} AND order_time < #{localEndTime}")
    Double getSumMoney(LocalDateTime localBeginTime, LocalDateTime localEndTime);

    /*
     * 查询指定状态的订单
     * */
    @Select("SELECT COUNT(id) FROM orders WHERE status = #{status}")
    Integer getCountByStatus(int i);
}
